from django.db import models
from datetime import datetime
from django.db.models import Q
from django.utils import timezone


# 请求记录表
class RequestLogTable(models.Model):
    ip = models.CharField(max_length=30, db_column='IP地址', verbose_name='IP地址')
    userAgent = models.CharField(max_length=200, db_column='请求头', verbose_name='请求头')
    httpRefer = models.CharField(max_length=100, db_column='跳转的网页', verbose_name='跳转的网页')
    requestTime = models.DateTimeField(default=timezone.now, db_column='请求时间', verbose_name='请求时间')
    requestPath = models.CharField(max_length=50, db_column='请求路径', default='/', verbose_name='请求路径')

    country = models.CharField(max_length=20, db_column='国家', verbose_name='国家', default='')
    region = models.CharField(max_length=20, db_column='省份', verbose_name='省份', default='')
    city = models.CharField(max_length=20, db_column='城市', verbose_name='城市', default='')
    latitude = models.CharField(max_length=20, db_column='纬度', verbose_name='纬度', default='')
    longitude = models.CharField(max_length=20, db_column='经度', verbose_name='经度', default='')
    timezone = models.CharField(max_length=20, db_column='时区', verbose_name='时区', default='')
    isp = models.CharField(max_length=30, db_column='网络服务提供者', verbose_name='网络服务提供者', default='')

    # ['IP地址', '请求头', '跳转的网页', '请求时间', '请求路径','国家','省份','城市','纬度','经度','时区','网络服务提供者']

    class Meta:
        db_table = '请求记录'
        verbose_name = verbose_name_plural = db_table

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    # 获取某天或某月或某年的访问量
    @classmethod
    def get_views(cls, year=None, moth=None, day=None):
        request_li = RequestLogTable.objects
        if year:
            request_li = request_li.filter(requestTime__year=year)
        if moth:
            request_li = request_li.filter(requestTime__month=moth)

        if day:
            request_li = request_li.filter(requestTime__day=day)
        ip_li = request_li.values_list('ip').distinct()
        return ip_li.count()

    @classmethod
    def create_request_log(cls, ip, userAgent, httpRefer, requestPath, country, region, city, latitude, longitude,
                           timezone, isp):
        requestLog = RequestLogTable()
        requestLog.ip = ip
        requestLog.userAgent = userAgent
        requestLog.httpRefer = httpRefer
        requestLog.requestPath = requestPath
        requestLog.country = country
        requestLog.region = region
        requestLog.city = city
        requestLog.latitude = latitude
        requestLog.longitude = longitude
        requestLog.timezone = timezone
        requestLog.isp = isp

        requestLog.save()


# 错误日志记录表
class ErrTable(models.Model):
    # 请求的ip
    ip = models.CharField(max_length=50, db_column='请求的ip', verbose_name='请求的ip')

    # 请求的路径
    path = models.CharField(max_length=200, db_column='请求的路径', verbose_name='请求的路径')

    # 请求的日期
    date = models.DateTimeField(auto_now=True, db_column='请求的日期', verbose_name='请求的日期')

    # 请求头
    userAgent = models.CharField(max_length=200, db_column='请求头', verbose_name='请求头')

    # 错误原因
    reason = models.CharField(max_length=500, db_column='错误原因', verbose_name='错误原因')

    class Meta:
        db_table = '错误日志'
        verbose_name = verbose_name_plural = db_table
        ordering = ['date']
